{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5b1cf804",
   "metadata": {},
   "source": [
    "# 基于 MindQuantum 0.7.0 实现半导体双量子点下的双量子比特 Grover 算法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7f0c9fbc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1¦11⟩\n"
     ]
    }
   ],
   "source": [
    "from mindquantum import *\n",
    "\n",
    "circ = Circuit()\n",
    "circ += H.on(0)\n",
    "circ += H.on(1)\n",
    "circ += Z.on(1,0)\n",
    "circ += H.on(0)\n",
    "circ += H.on(1)\n",
    "circ += Z.on(0)\n",
    "circ += Z.on(1)\n",
    "circ += Z.on(1,0)\n",
    "circ += H.on(0)\n",
    "circ += H.on(1)\n",
    "\n",
    "sim = Simulator('projectq', 2)\n",
    "sim.apply_circuit(circ)\n",
    "print(sim.get_qs(ket=True))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e583d2d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2.6125416398212854e-05, 3.064878890397258e-05, 3.449741938457886e-05, 0.9999087283753169]\n"
     ]
    }
   ],
   "source": [
    "import copy\n",
    "import numpy as np\n",
    "import mindspore as ms\n",
    "from numpy import kron\n",
    "from mindquantum import *\n",
    "from scipy.linalg import expm\n",
    "from mindspore.ops import operations\n",
    "from mindspore import nn, ops, Tensor, context\n",
    "from mindspore.common.parameter import Parameter\n",
    "from mindspore.common.initializer import initializer  \n",
    "from mindspore.nn import Adam, TrainOneStepCell, LossBase\n",
    "ms.context.set_context(mode=ms.context.PYNATIVE_MODE, device_target=\"CPU\")\n",
    "ms.set_seed(1)\n",
    "np.random.seed(1)\n",
    "\n",
    "s_x = X.matrix()\n",
    "s_z = Z.matrix()\n",
    "one = I.matrix()\n",
    "dt = np.pi/2\n",
    "ddt = np.pi/10\n",
    "\n",
    "def _matrix_(coeff):\n",
    "    return expm(-1j*(coeff*s_z+s_x)*dt)\n",
    "\n",
    "def _diff_matrix_(coeff):\n",
    "    return -1j*_matrix_(coeff)@(s_z*dt)\n",
    "\n",
    "def _matrix_0(coeff):\n",
    "    return expm(-1j*(coeff*s_z+s_x)*ddt)\n",
    "\n",
    "def _diff_matrix_0(coeff):\n",
    "    return -1j*_matrix_0(coeff)@(s_z*ddt)\n",
    "\n",
    "def _matrix_c_0(coeff):\n",
    "    return expm(-1j*(coeff*kron(s_z, one) + kron(one, s_z) + kron(s_x, one) + kron(one, s_x) + coeff*kron(s_z-one, s_z-one))*5*ddt)\n",
    "\n",
    "def _diff_matrix_c_0(coeff):\n",
    "    return -1j*_matrix_c_0(coeff)@((kron(s_z, one) + kron(s_z-one, s_z-one)) * 5*ddt)\n",
    "\n",
    "def _matrix_c_1(coeff):\n",
    "    return expm(-1j*(kron(s_z, one) + coeff*kron(one, s_z) + kron(s_x, one) + kron(one, s_x) + coeff*kron(s_z-one, s_z-one))*5*ddt)\n",
    "\n",
    "def _diff_matrix_c_1(coeff):\n",
    "    return -1j*_matrix_c_1(coeff)@((kron(one, s_z) + kron(s_z-one, s_z-one)) *  5*ddt)\n",
    "\n",
    "gate = gene_univ_parameterized_gate('gete', _matrix_, _diff_matrix_) # dt=pi/2\n",
    "gate_0 = gene_univ_parameterized_gate('gete_0', _matrix_0, _diff_matrix_0) # ddt=pi/10\n",
    "gate_c_0 = gene_univ_parameterized_gate('gete_c_0', _matrix_c_0, _diff_matrix_c_0) # ddt=pi/10\n",
    "gate_c_1 = gene_univ_parameterized_gate('gete_c_1', _matrix_c_1, _diff_matrix_c_1) # ddt=pi/10\n",
    "\n",
    "cz_params = np.array([1.5472503,  1.4179231,  1.540713,   1.9724044,  1.9253408,  1.3879265,\n",
    "                     0.8130467,  0.76446086, 1.2703444,  1.8553745,  1.0291328,  1.2492974,\n",
    "                     0.7880994,  0.3026381,  0.31203356, 0.30834132, 0.9533752,  1.3802187,\n",
    "                     1.270656,   0.5646567,  0.94619316, 0.97377133, 1.9658349,  0.83277696,\n",
    "                     1.0190777,  0.90001523, 0.26008993, 0.16526282, 0.22249524, 1.1596956,\n",
    "                     1.5285202,  0.4919534,  0.01645389, 0.02608137, 0.6504683,  0.31325826,\n",
    "                     0.4486266,  0.8677286,  1.3571227,  1.4995408,  1.1248059,  0.5996333,\n",
    "                     0.32797617, 0.54987127])\n",
    "\n",
    "h_params = np.array([0.93665886, 0.24112344, 3.1801345,  1.1307619,  1.5094017,  3.022603,\n",
    "                     0.45939285, 2.7383144,  1.6714032,  2.1061618,  2.3177178,  1.2582982])\n",
    "\n",
    "z_params = np.array([1.6117827,  1.3258268,  0.0498501,  2.3190951,  0.27652496, 1.5626596,\n",
    "                     0.70895815, 0.61659425, 1.8357016,  1.4761814,  2.117067,   0.6488876 ])\n",
    "\n",
    "def cz_circ():\n",
    "    circ_ = Circuit()\n",
    "    circ_ += BarrierGate()\n",
    "    circ_ += Circuit([gate_0(param).on(0) for param in cz_params[:10]])\n",
    "    circ_ += Circuit([gate_0(0).on(1) for i in range(10)])\n",
    "    circ_ += BarrierGate()\n",
    "    circ_ += Circuit([gate_0(0).on(0) for i in range(10)])\n",
    "    circ_ += Circuit([gate_0(param).on(1) for param in cz_params[10:20]])\n",
    "    circ_ += Circuit([gate_c_0(cz_params[20]).on([1,0]), gate_c_0(cz_params[21]).on([1,0])])\n",
    "    circ_ += Circuit([gate_c_1(cz_params[22]).on([1,0]), gate_c_1(cz_params[23]).on([1,0])])\n",
    "    circ_ += Circuit([gate_0(param).on(0) for param in cz_params[24:34]])\n",
    "    circ_ += Circuit([gate_0(0).on(1) for i in range(10,20)])\n",
    "    circ_ += BarrierGate()\n",
    "    circ_ += Circuit([gate_0(0).on(0) for i in range(10)])\n",
    "    circ_ += Circuit([gate_0(param).on(1) for param in cz_params[34:]])\n",
    "    circ_ += BarrierGate()\n",
    "    return circ_\n",
    "\n",
    "def i_circ(qubit=0):\n",
    "    circ_ = Circuit()\n",
    "    circ_ += BarrierGate()\n",
    "    for i in range(12):\n",
    "        circ_ += gate(0).on(qubit)\n",
    "    return circ_\n",
    "\n",
    "def h_circ(qubit=0):\n",
    "    circ_ = Circuit()\n",
    "    circ_ += BarrierGate()\n",
    "    for i in range(12):\n",
    "        circ_ += gate(h_params[i]).on(qubit)\n",
    "    return circ_\n",
    "\n",
    "def z_circ(qubit=0):\n",
    "    circ_ = Circuit()\n",
    "    circ_ += BarrierGate()\n",
    "    for i in range(12):\n",
    "        circ_ += gate(z_params[i]).on(qubit)\n",
    "    return circ_\n",
    "\n",
    "circ = Circuit()\n",
    "\n",
    "circ += h_circ(0)\n",
    "circ += i_circ(1)\n",
    "circ += i_circ(0)\n",
    "circ += h_circ(1)\n",
    "\n",
    "circ += cz_circ()\n",
    "\n",
    "circ += h_circ(0)\n",
    "circ += i_circ(1)\n",
    "circ += i_circ(0)\n",
    "circ += h_circ(1)\n",
    "\n",
    "circ += z_circ(0)\n",
    "circ += i_circ(1)\n",
    "circ += i_circ(0)\n",
    "circ += z_circ(1)\n",
    "\n",
    "circ += cz_circ()\n",
    "\n",
    "circ += h_circ(0)\n",
    "circ += i_circ(1)\n",
    "circ += i_circ(0)\n",
    "circ += h_circ(1)\n",
    "\n",
    "\n",
    "sim = Simulator('projectq', 2)\n",
    "sim.apply_circuit(circ)\n",
    "state = sim.get_qs()\n",
    "distribution = []\n",
    "for i in range(len(state)):\n",
    "    distribution.append((state[i].conj()*state[i]).real)\n",
    "print(distribution)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
